package com.aphrodite.ui.widgets;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.aphrodite.util.StringUtil;

/**
 * Animated notification message box.
 * 
 * 
 * @author kentelt
 * 
 */
public final class Toast extends TextEffect {
	private int bgColorTrak;
	private int fgColorTrak;
	private Image icon;
	private volatile int noteX;
	private volatile int noteY;
	private Image offScrImg;

	public Toast(Theme theme, String message) {
		this(theme, message, 1, 1, 100);
	}

	/**
	 * 
	 * @param message
	 * @param x
	 *            Point from where the notification should pop up
	 * @param y
	 *            Point from where the notification should pop up
	 * @param width
	 *            of the notification box
	 */
	public Toast(Theme theme, String message, int x, int y, int width) {
		super(theme, width, 0);
		this.x = x;
		this.y = y;
		effectInterval = 3000;
		this.text = message;
		updateModel();
		isVisible = false;
	}

	protected void doAniTask() {
		while (noteY > 0) {
			noteY--;
			reDrawWidget();
		}
		doEffect = false;
	}

	protected void doPostAniTask() {
		while (noteY < height + 1) {
			noteY++;
			reDrawWidget();
		}
		isVisible = false;
	}

	public void render(Graphics g) {
		if (!isVisible)
			return;
		capture(g, width, height, false);
		/*
		 * This is a workaround to detect color changes in the theme. since the
		 * rendering is done offscreen, the offscreen buffer should be updated
		 * when there are changes to the theme
		 */
		if (fgColorTrak != theme.fgColor || bgColorTrak != theme.bgColor) {
			updateModel();
		}
		g.drawImage(offScrImg, noteX, noteY, 0);
		release(g, width, height, false);

	}

	/**
	 * Set an image for the notification box
	 * 
	 * @param image
	 */
	public void setImage(Image image) {
		this.icon = image;
		updateModel();
		reDrawWidget();
		stateChanged(this);
	}

	public void start() {
		if (!isVisible) {
			isVisible = true;
			super.start();
		}
	}

	protected void updateModel() {
		fgColorTrak = theme.fgColor;
		bgColorTrak = theme.bgColor;
		String[] lines = StringUtil.doTextWrap(text, theme.widgetFont,
				(width - 4) - (icon != null ? icon.getWidth() + 2 : 0), width - 4);
		System.out.println("Font height " + theme.widgetFont.getHeight());
		/* Top padding */
		height = 2;
		int totalLines = lines.length;
		int startLine = 0;

		if (icon != null) {
			if (icon.getHeight() > theme.widgetFont.getHeight()) {
				height += icon.getHeight() + 2;
				/* The first line will be contained along with image */
				totalLines--;
			}
		}
		height += totalLines * theme.widgetFont.getHeight();

		offScrImg = Image.createImage(width, height);
		Graphics g = offScrImg.getGraphics();
		theme.renderBG(g, width, height);

		g.setFont(theme.widgetFont);
		g.setColor(theme.fgColor);
		int currY = 2;
		if (icon != null) {
			g.drawImage(icon, 2, 2, 0);
			g
					.drawString(lines[0], 4 + icon.getWidth(),
							2 + (icon.getWidth() / 2 - theme.widgetFont.getHeight() / 2), 0);
			startLine = 1;
			currY += icon.getHeight() + 2;
		}
		for (int i = startLine; i < lines.length; i++) {
			g.drawString(lines[i], 2, currY, 0);
			currY += theme.widgetFont.getHeight();
		}
		noteX = 0;
		noteY = height - 1;

	}

}
